package com.vrv.netmgr.config;

import java.util.HashMap;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.vrv.common.GlobalConstants;
import com.vrv.common.util.StringUtils;
import com.vrv.netmgr.R;
import com.vrv.netmgr.persistent.LoginSharedPreferencesHelper;
import com.vrv.netmgr.usermgr.UserManager;
import com.vrv.netmgr.vo.LoginInfo;

/**
 * @Description 用户设置
 * 
 * @author gaopei
 * @date
 */
@SuppressLint("HandlerLeak")
public class UserSettingsActivity extends Activity {

	/**
	 * 登录
	 */
	private static final int SAVE = 1;
	/**
	 * 账号
	 */
	private EditText accountExt;
	/**
	 * 密码
	 */
	private EditText passwdExt;
	/**
	 * 新密码
	 */
	private EditText newPasswdExt;
	/**
	 * 确认新密码
	 */
	private EditText confirmPwdExt;
	/**
	 * 保存
	 */
	private Button saveBtn;
	/**
	 * 重置
	 */
	private Button resetBtn;
	/**
	 * 等待对话框
	 */
	private ProgressDialog progressDialog;
	/**
	 * 登录信息
	 */
	private boolean isSuccess;
	private LoginInfo loginInfo;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		setContentView(R.layout.modify_passwd);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
				R.layout.common_title);
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

		init();
	}
	
	private void setTitle(){
		TextView title = (TextView) findViewById(R.id.title_text);
		title.setText("用户设置");

		/**
		 * 返回按钮
		 */
		ImageButton backBtn = (ImageButton) findViewById(R.id.backBtn);
		backBtn.setVisibility(View.VISIBLE);
		backBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		});
	}

	private void init() {
		setTitle();
		accountExt = (EditText) findViewById(R.id.accountExt);
		loginInfo = LoginSharedPreferencesHelper
				.getLoginInfo(getApplicationContext());
		accountExt.setText(loginInfo.getLoginName());
		passwdExt = (EditText) findViewById(R.id.passwdExt);
		newPasswdExt = (EditText) findViewById(R.id.newPasswdExt);
		confirmPwdExt = (EditText) findViewById(R.id.confirmPwdExt);

		saveBtn = (Button) findViewById(R.id.saveBtn);
		saveBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				if (prepare()) {
					progressDialog = ProgressDialog
							.show(UserSettingsActivity.this, "", "保存中...",
									true, true);
					new Thread(new Runnable() {
						@Override
						public void run() {
							save();
							handler.sendEmptyMessage(SAVE);
						}
					}).start();
				}
			}
		});

		resetBtn = (Button) findViewById(R.id.resetBtn);
		resetBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				passwdExt.setText("");
				newPasswdExt.setText("");
				confirmPwdExt.setText("");
			}
		});		
	}
	
	/**
	 * 处理器
	 */
	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			progressDialog.dismiss();
			switch (msg.what) {
			case SAVE:
				if (isSuccess) {
					resetBtn.performClick();
					Toast.makeText(UserSettingsActivity.this, "保存成功",
							Toast.LENGTH_SHORT).show();

				} else {
					Toast.makeText(UserSettingsActivity.this, "网络连接失败",
							Toast.LENGTH_LONG).show();
				}
				break;
			}
		}
	};

	/**
	 * 准备登录
	 * 
	 * @return
	 */
	private boolean prepare() {
		String oldPasswd = passwdExt.getText().toString().trim();
		if (StringUtils.isBlank(oldPasswd)) {
			Toast.makeText(UserSettingsActivity.this, "原密码不能为空.", Toast.LENGTH_SHORT).show();
			return false;
		}
		String newPasswd = newPasswdExt.getText().toString().trim();
		if (StringUtils.isBlank(newPasswd)) {

			Toast.makeText(UserSettingsActivity.this, "新密码不能为空.", Toast.LENGTH_SHORT).show();
			return false;
		}
		String confirmPwd = confirmPwdExt.getText().toString().trim();
		if (StringUtils.isBlank(confirmPwd)) {
			Toast.makeText(UserSettingsActivity.this, "确认新密码不能为空.", Toast.LENGTH_SHORT).show();
			return false;
		}
		if (!newPasswd.equals(confirmPwd)) {
			Toast.makeText(UserSettingsActivity.this, "确认新密码与新密码不同.", Toast.LENGTH_SHORT).show();
			return false;
		}
		String passwd = loginInfo.getLoginPasswd();
		if (!oldPasswd.equals(passwd)) {
			Toast.makeText(UserSettingsActivity.this, "原密码错误.", Toast.LENGTH_SHORT).show();
			return false;
		}
		return true;
	}

	private void save() {
		Map<String, String> map = new HashMap<String, String>();
		map.put(GlobalConstants.USER_ID, loginInfo.getID());
		map.put(GlobalConstants.USER_NAME, loginInfo.getLoginName());
		map.put(GlobalConstants.PASSWORD, newPasswdExt.getText().toString());
		loginInfo.setLoginPasswd(newPasswdExt.getText().toString());
		isSuccess = new UserManager().modifyPasswd(getApplicationContext(), map);		
	}

	@Override
	protected void onPause() {
		if (progressDialog != null) {
			progressDialog.dismiss();
		}
		super.onPause();
	}

}
